package com.xiefayang.l_personalTask02;

import java.util.List;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

/**
 * 个人任务 : 
 * 
 *  	这里使用类的方式指定任务的办理人
 *   	只需要在personalTask02.bpmn中任务节点中配置listener
 *   
 *   还介绍了一个任务办理人从一个人转移到另一个人身上的方法(认领任务)
 */
public class TaskTest {
	
	ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
	
	/**
	 * 部署流程定义
	 */
	@Test
	public void deploymentProcessDefinition(){
		
		// RepositoryService: 与流程定义和部署对象相关的Service
		Deployment deployment = processEngine.getRepositoryService() 
				.createDeployment() //创建一个部署对象
				.name("个人任务") //添加部署的名称
				.addClasspathResource("diagrams/personalTask02.bpmn") //从classpath中加载资源, 一次只能加载一个
				.addClasspathResource("diagrams/personalTask02.png")
				.deploy(); //完成部署
		
		System.out.println("部署ID: " + deployment.getId());
		System.out.println("部署名称: " + deployment.getName());
	}
	
	
	/**
	 * 启动流程实例
	 */
	@Test
	public void startProcessInstance() {
		
		String processDefinitionKey = "task";
		ProcessInstance processInstance = this.processEngine.getRuntimeService().startProcessInstanceByKey(processDefinitionKey);
		System.out.println("流程实例ID: " + processInstance.getId());
		System.out.println("流程定义ID: " + processInstance.getProcessDefinitionId());
	}
	
	
	/**
	 * 查询当前人的个人任务 
	 */
	@Test
	public void findMyPerosonalTask() {
		
		String assignee = "陈敏";
		List<Task> taskList = this.processEngine.getTaskService()
				.createTaskQuery().taskAssignee(assignee).orderByTaskCreateTime().asc().list();
		
		for (Task task: taskList) {
			
			System.out.println("任务ID:"+task.getId());
			System.out.println("任务名称:"+task.getName());
			System.out.println("任务的创建时间:"+task.getCreateTime());
			System.out.println("任务的办理人:"+task.getAssignee());
			System.out.println("流程实例ID："+task.getProcessInstanceId());
			System.out.println("执行对象ID:"+task.getExecutionId());
			System.out.println("流程定义ID:"+task.getProcessDefinitionId());
			System.out.println("########################################################");
		}
	}
	
	
	/**
	 * 完成我的任务
	 */
	@Test
	public void completeMyPersonalTask() {
		
		//任务ID
		String taskId = "45004";
		this.processEngine.getTaskService().complete(taskId);
		System.out.println("完成任务: " + taskId);
	}
	
	
	/**
	 * 分配个人任务从一个人到另一个人(认领任务) 
	 */
	@Test
	public void setAssigneeTask() {
		
		String taskId = "45004";
		String userId = "陈敏";
		this.processEngine.getTaskService().setAssignee(taskId, userId);
	}
}
